home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Devices / dev_examples / Demo_Dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  10.3 KB  |  289 lines

  1. /* Demo_Dump.c
  2.  *
  3.  * Simple example of dumping a rastport to the printer, changing
  4.  * printer preferences programmatically and handling error codes.
  5.  *
  6.  * Compile with SAS C 5.10a. lc -cfist -v -L Demo_Dump
  7.  *
  8.  * Requires Kickstart V37
  9.  * Run from CLI only
  10.  */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <exec/ports.h>
  15. #include <devices/printer.h>
  16. #include <devices/prtbase.h>
  17. #include <dos/dos.h>
  18. #include <intuition/intuition.h>
  19. #include <intuition/screens.h>
  20. #include <graphics/displayinfo.h>
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/alib_protos.h>
  24. #include <clib/alib_stdio_protos.h>
  25. #include <clib/graphics_protos.h>
  26. #include <clib/intuition_protos.h>
  27.  
  28. struct IntuitionBase *IntuitionBase;
  29. struct GfxBase *GfxBase;
  30.  
  31. union printerIO
  32. {
  33.     struct IOStdReq    ios;
  34.     struct IODRPReq    iodrp;
  35.     struct IOPrtCmdReq iopc;
  36. };
  37.  
  38. struct EasyStruct reqES =
  39. {
  40.     sizeof(struct EasyStruct), 0, "DemoDump",
  41.     "%s",
  42.     NULL,
  43. };
  44.  
  45. /* Possible printer.device and I/O errors */
  46. static UBYTE *ErrorText[] =
  47. {
  48.     "PDERR_NOERR",
  49.     "PDERR_CANCEL",
  50.     "PDERR_NOTGRAPHICS",
  51.     "INVERTHAM",                /* OBSOLETE */
  52.     "BADDIMENSION",
  53.     "DIMENSIONOVFLOW",  /* OBSOLETE */
  54.     "INTERNALMEMORY",
  55.     "BUFFERMEMORY",
  56.     /* IO_ERRs */
  57.     "IOERR_OPENFAIL",
  58.     "IOERR_ABORTED",
  59.     "IOERR_NOCMD",
  60.     "IOERR_BADLENGTH"
  61. };
  62.  
  63. /* Requester Action text */
  64. static UBYTE *ActionText[] =
  65. {
  66.     "OK|CANCEL",
  67.     "Continue",
  68.     "Abort",
  69. };
  70.  
  71. #define OKCANCELTEXT 0
  72. #define CONTINUETEXT 1
  73. #define ABORTTEXT    2
  74.  
  75. VOID main(VOID);
  76.  
  77. VOID main(VOID)
  78. {
  79. struct MsgPort  *PrinterMP;
  80. union printerIO *PIO;
  81. struct PrinterData *PD;
  82. struct PrinterExtendedData *PED;
  83. struct Screen *pubscreen;
  84. struct ViewPort *vp;
  85. STRPTR textbuffer;
  86. LONG modeID, i,j;
  87. ULONG dcol[5], drow[5];
  88. ULONG signal;
  89.  
  90. /* Fails silently if not V37 or greater. Nice thing to do would be to put up
  91.  * a V33 requester of course.
  92.  */
  93.  
  94. /* Set up once */
  95. reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  96.  
  97. if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37))
  98.   {
  99.   /* Using graphics.library to get the displaymodeID of the public screen,
  100.    * which we'll pass to the printer.device.
  101.    */
  102.   if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37))
  103.     {
  104.     if (textbuffer = (STRPTR)AllocMem(256, MEMF_CLEAR))
  105.       {
  106.       /* Create non-public messageport. Since we depend on V37 already, we'll
  107.        * use the new Exec function.
  108.        */
  109.       if (PrinterMP = CreateMsgPort())
  110.         {
  111.         /* Allocate printerIO union */
  112.         if (PIO = (union printerIO *)CreateExtIO(PrinterMP, sizeof(union printerIO)))
  113.           {
  114.           /* Open the printer.device */
  115.           if (!(OpenDevice("printer.device",0,(struct IORequest *)PIO,0)))
  116.             {
  117.             /* Yahoo, we've got it.
  118.              * We'll use the PrinterData structure to get to the the printer
  119.              * preferences later on. The PrinterExtendedData structure will
  120.              * reflect the changes we'll make to the preferences.
  121.              */
  122.  
  123.             PD = (struct PrinterData *)PIO->iodrp.io_Device;
  124.             PED = (struct PrinterExtendedData *)&PD->pd_SegmentData->ps_PED;
  125.  
  126.             /* We're all set. We'll grab the default public screen (normally
  127.              * Workbench) and see what happens when we dump it with different
  128.              * densities.
  129.              * Next we'll put up a nice requester for the user and ask if
  130.              * (s)he wants to actually do the dump.
  131.              */
  132.  
  133.             if (pubscreen = LockPubScreen(NULL))
  134.               {
  135.               vp = &(pubscreen->ViewPort);
  136.               /* Use graphics.library/GetVPModeID() to get the ModeID of the screen. */
  137.               if ((modeID = GetVPModeID(vp)) != INVALID_ID)
  138.                 {
  139.                 /* Seems we got a valid ModeID for the default public screen (surprise).
  140.                  * Do some fake screen dumps with densities 1, 3, 5 and 7. Depending on
  141.                  * the driver, one or more may be the same.
  142.                  */
  143.  
  144.                 /* Fill in those parts of the IODRPRequest which won't change */
  145.                 PIO->iodrp.io_Command = PRD_DUMPRPORT;
  146.                 PIO->iodrp.io_RastPort = &(pubscreen->RastPort);
  147.                 PIO->iodrp.io_ColorMap = vp->ColorMap;
  148.                 PIO->iodrp.io_Modes = modeID;
  149.                 PIO->iodrp.io_SrcX = pubscreen->LeftEdge;
  150.                 PIO->iodrp.io_SrcY = pubscreen->TopEdge;
  151.                 PIO->iodrp.io_SrcWidth = pubscreen->Width;
  152.                 PIO->iodrp.io_SrcHeight = pubscreen->Height;
  153.  
  154.                 for (i = 1,j=0; i < 8; i+=2,j++)
  155.                   {
  156.                   /* On return these will contain the actual dump dimension */
  157.                   PIO->iodrp.io_DestCols = 0;
  158.                   PIO->iodrp.io_DestRows = 0;
  159.                   /* We'll simply change our local copy of the
  160.                    * Preferences structure. Likewise we could change
  161.                    * all printer-related preferences.
  162.                    */
  163.                   PD->pd_Preferences.PrintDensity = i;
  164.                   PIO->iodrp.io_Special = SPECIAL_NOPRINT|SPECIAL_ASPECT;
  165.  
  166.                   /* No need to do asynchronous I/O here */
  167.                   DoIO((struct IORequest *)PIO);
  168.  
  169.                   if (PIO->iodrp.io_Error == 0)
  170.                     {
  171.                     dcol[j] = PIO->iodrp.io_DestCols;
  172.                     drow[j] = PIO->iodrp.io_DestRows;
  173.                     }
  174.                   else
  175.                     {
  176.                     j = PIO->iodrp.io_Error;
  177.                     if (j < 0)
  178.                       j = j * -1 + 7;
  179.  
  180.                     sprintf(textbuffer, "Error: %s\n", ErrorText[j]);
  181.                     reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  182.                     EasyRequest(NULL, &reqES, NULL, textbuffer);
  183.                     break;
  184.                     }
  185.                   }
  186.                 /* Simple, lazy way to check if we encountered any problems */
  187.                 if (i == 9)
  188.                   {
  189.                   /* Build an 'intelligent' requester */
  190.                   sprintf(textbuffer,
  191.                     "%s: %5ld x %5ld\n%s: %5ld x %5ld\n%s: %5ld x%5ld\n%s: %5ld x %5ld\n\n%s",
  192.                     "Density 1", dcol[0], drow[0],
  193.                     "Density 3", dcol[1], drow[1],
  194.                     "Density 5", dcol[2], drow[2],
  195.                     "Density 7", dcol[3], drow[3],
  196.                     "Print screen at highest density?");
  197.                   reqES.es_GadgetFormat = ActionText[OKCANCELTEXT];
  198.  
  199.                   /* Obviously the choice presented to the user here is a very
  200.                    * simple one. To print or not to print. In a real life
  201.                    * application, a requester could be presented, inviting
  202.                    * the user to select density, aspect, dithering etc.
  203.                    * The fun part is, of course, that the user can, to a certain
  204.                    * degree, be informed about the effects of her/his selections.
  205.                    */
  206.                   if (EasyRequest(NULL, &reqES, NULL, textbuffer))
  207.                     {
  208.                     /* We've still got the density preference set to the highest
  209.                      * density, so no need to change that.
  210.                      * All we do here is re-initialize io_DestCols/Rows and remove
  211.                      * the SPECIAL_NOPRINT flag from io_Special.
  212.                      */
  213.                     PIO->iodrp.io_DestCols = 0;
  214.                     PIO->iodrp.io_DestRows = 0;
  215.                     PIO->iodrp.io_Special &= ~SPECIAL_NOPRINT;
  216.  
  217.                     /* Always give the user a change to abort.
  218.                      * So we'll use SendIO(), instead of DoIO(), to be asynch and
  219.                      * catch a possible user request to abort printing. Normally,
  220.                      * the user would be presented with a nice, fat, ABORT requester.
  221.                      * However, since this example doesn't even open a window, and is
  222.                      * basically a 'GraphicDumpDefaultPubscreen' equivalent, we'll use
  223.                      * CTRL-C as the user-abort. Besides that, got to keep it short.
  224.                      */
  225.                     SendIO((struct IORequest *)PIO);
  226.  
  227.                     /* Now Wait() for either a user signal (CTRL-C) or a signal from
  228.                      * the printer.device
  229.                      */
  230.                     signal = Wait(1 << PrinterMP->mp_SigBit | SIGBREAKF_CTRL_C);
  231.  
  232.                     if (signal & SIGBREAKF_CTRL_C)
  233.                       {
  234.                       /* User wants to abort */
  235.                       AbortIO((struct IORequest *)PIO);
  236.                       WaitIO((struct IORequest *)PIO);
  237.                       }
  238.  
  239.                     if (signal & (1 << PrinterMP->mp_SigBit))
  240.                       {
  241.                       /* printer is either ready or an error has occurred */
  242.                       /* Remove any messages */
  243.                       while(GetMsg(PrinterMP));
  244.                       }
  245.                     /* Check for errors (in this case we count user-abort as an error) */
  246.                     if (PIO->iodrp.io_Error != 0)
  247.                       {
  248.                       j = PIO->iodrp.io_Error;
  249.                       if (j < 0)
  250.                         j = j * -1 + 7;
  251.                       sprintf(textbuffer, "Error: %s\n", ErrorText[j]);
  252.                       reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  253.                       EasyRequest(NULL, &reqES, NULL, textbuffer);
  254.                       }
  255.  
  256.                     } /* else user doesn't want to print */
  257.                   }
  258.                 }
  259.               else
  260.                 /* Say what? */
  261.                 EasyRequest(NULL, &reqES, NULL, "Invalid ModeID\n");
  262.               UnlockPubScreen(NULL, pubscreen);
  263.               }
  264.             else
  265.               EasyRequest(NULL, &reqES, NULL, "Can't lock Public Screen\n");
  266.  
  267.             CloseDevice((struct IORequest *)PIO);
  268.             }
  269.           else
  270.             EasyRequest(NULL, &reqES, NULL, "Can't open printer.device\n");
  271.  
  272.           DeleteExtIO((struct IORequest *)PIO);
  273.           }
  274.         else
  275.           EasyRequest(NULL, &reqES, NULL, "Can't create Extented I/O Request\n");
  276.         DeleteMsgPort(PrinterMP);
  277.         }
  278.       else
  279.         EasyRequest(NULL, &reqES, NULL, "Can't create Message port\n");
  280.       /* else Out of memory? 256 BYTES? */
  281.       FreeMem(textbuffer,256);
  282.       }
  283.     CloseLibrary(GfxBase);
  284.     } /* else MAJOR confusion */
  285.   CloseLibrary(IntuitionBase);
  286.   }
  287. }
  288.  
  289.